package com.hongxun.design.utils.wifiutil;


import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;

import com.hongxun.design.BroadCastReceivers.WifiScanRsultBroadCast;
import com.hongxun.design.BroadCastReceivers.WifiStateBroadCast;
import com.hongxun.design.BroadCastReceivers.WifiStateBroadCast.WifiBroadCastOperations;
import com.hongxun.design.MyApp;
import com.hongxun.design.mytools.MyLog;


/**
 * Created by zerdoor_pc on 2016/4/18.
 */
public class WifiHostSearch {
    public WifiStateBroadCast wifiStateReceiver;
    private static WifiHostSearch instance = null;
    private Context context;
    private  WifiBroadCastOperations operations;
    private WifiScanRsultBroadCast wifiScanReceiver;
    WifiHostSearch(Context context, WifiBroadCastOperations operations) {
        this.context = context;
        this.operations = operations;
    }

    public static WifiHostSearch getInstance(Context context) {

        if (instance == null) {
            instance = new WifiHostSearch(context, (WifiBroadCastOperations) context);
        }
        instance.context=context;
        return instance;
    }

    public void clear(){
        instance=null;
    }
    // 扫描Wifi热点
    public void scanWifiHot() {
        WifiHostUtil.getInstance(context).closeWifi();
        MyLog.L(MyApp.AppName + "into wifiHotScan()");
        if (!WifiHostUtil.getInstance(context).wifiIsOpen()) {//检查wifi是否打开
            MyLog.L(MyApp.AppName + "out wifiHotScan() wifi is not open!");
            registerWifiStateBroadcast("");
            wifiStateReceiver.setOpType(OpretionsType.SCAN);
            WifiHostUtil.getInstance(context).openWifi();
        } else {
            MyLog.L(MyApp.AppName + "out wifiHotScan() wifi is  open!");
            scanNearWifiHots();
        }
        MyLog.L(MyApp.AppName + "out wifiHotScan()");
    }

    // 注册wifi 状态监听广播
    public void registerWifiStateBroadcast(String SSID) {
        IntentFilter filter = new IntentFilter();
        if (wifiStateReceiver == null) {
            wifiStateReceiver = new WifiStateBroadCast(operations, SSID);
        }
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        context.registerReceiver(wifiStateReceiver, filter);
    }

    // 注册wifi 扫描结果监听广播
    private void registerWifiScanBroadCast() {
        IntentFilter filter = new IntentFilter();
        if (wifiScanReceiver == null) {
            wifiScanReceiver = new WifiScanRsultBroadCast(operations);
        }
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        context.registerReceiver(wifiScanReceiver, filter);
    }
    // 去掉wifi扫描结果监听
    public void unRegisterWifiScanBroadCast() {
        if (wifiScanReceiver != null) {
            context.unregisterReceiver(wifiScanReceiver);
            wifiScanReceiver = null;
        }
    }
    // 去掉wifi状态广播监听
    public void unRegisterWifiStateBroadCast() {
        if (wifiStateReceiver != null) {
            context.unregisterReceiver(wifiStateReceiver);
            wifiStateReceiver = null;
        }
    }

    // 搜索附近Wifi热点
    private void scanNearWifiHots() {
        MyLog.L(MyApp.AppName + "into scanNearWifiHots()");
        registerWifiScanBroadCast();
        WifiHostUtil.getInstance(context).mWifimanager.startScan();
        MyLog.L(MyApp.AppName + "out scanNearWifiHots()");
    }
}
